package com.ruoyi.web.controller.api.chat;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.system.domain.bo.PChatDetailsBo;
import com.ruoyi.system.domain.bo.PChatListBo;
import com.ruoyi.system.domain.vo.PChatDetailsVo;
import com.ruoyi.system.domain.vo.PChatListVo;
import com.ruoyi.system.service.IPChatDetailsService;
import com.ruoyi.system.service.IPChatListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zcc
 * @date 2022/8/22
 * @apiNote
 */
@Api(value = "消息模块", tags = {"前端-消息"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/chat")
public class ChatApiController {

    private final IPChatDetailsService iIPChatDetailsService;

    private final IPChatListService iIPChatListService;


    @ApiModelProperty(value = "消息列表-分页")
    @GetMapping("/get-chat-list")
    public R<List<PChatDetailsBo>> getUserIndex(PChatDetailsVo pChatDetailsVo) {
        List<PChatDetailsBo>  list1 =  iIPChatDetailsService.pChatDetailsList(pChatDetailsVo.getId(),pChatDetailsVo.getPage());
        for(PChatDetailsBo lisat: list1){
            if(String.valueOf(lisat.getUserId()).equals(String.valueOf(LoginHelper.getUserId()))){
                lisat.setType(1);
                lisat.setChatId(","+(pChatDetailsVo.getId()));
            }else{
                lisat.setType(0);
                lisat.setChatId(","+(pChatDetailsVo.getId()));
            }
        }
        return R.ok(list1);
    }

    @ApiOperation(value = "清除所有未读")
    @PostMapping("/chat-eliminate")
    public R<Map<String, Object>> userInfo(@Validated @RequestBody Map<String, Object> map) {
        PChatListBo pChatListBo = new PChatListBo();
        pChatListBo.setAnotherId(Long.valueOf(map.get("userIds").toString()));
        pChatListBo.setUserId(Long.valueOf(map.get("userid").toString()));
        List<PChatListVo> pChatListVos =  iIPChatListService.queryList(pChatListBo);
        if(pChatListVos.size() > 0 ){
            pChatListVos.get(0).setUnread(Long.valueOf("0"));
            iIPChatListService.updateByBo(BeanUtil.toBean(pChatListVos.get(0), PChatListBo.class));
        }
        PChatListBo pChatListBos = new PChatListBo();
        pChatListBo.setAnotherId(Long.valueOf(map.get("userid").toString()));
        pChatListBo.setUserId(Long.valueOf(map.get("userIds").toString()));
        List<PChatListVo> pChatListVo = iIPChatListService.queryList(pChatListBos);
        if(pChatListVo.size() > 0 ){
            pChatListVo.get(0).setUnread(Long.valueOf("0"));
            iIPChatListService.updateByBo(BeanUtil.toBean(pChatListVo.get(0), PChatListBo.class));
        }
        return R.ok();
    }


    @ApiModelProperty(value = "聊天列表")
    @GetMapping("/chatList")
    public R<List<Map<String, Object>>> getChatList(PChatDetailsVo pChatDetailsVo) {
        List<Map<String, Object>> map = iIPChatListService.chatLists(LoginHelper.getUserId(),pChatDetailsVo.getStatus());
        return R.ok(map);
    }



}
